/**
 * 
 */
package com.pig4cloud.pig.common.swagger.config;

/** 
* @version:v1.0
* @Description: 
* @author: jzhao
* @date: 2019年11月4日上午9:31:41
*/

import com.fasterxml.classmate.TypeResolver;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.data.domain.Pageable;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.AlternateTypeRuleConvention;
import springfox.documentation.schema.AlternateTypeRules;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * swaggger Pageable 参数定义
 */
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
@SuppressWarnings("SpringJavaAutowiringInspection")
public class PageableParamConfig {
    @Bean
    public AlternateTypeRuleConvention pageableConvention(final TypeResolver resolver) {
        return new AlternateTypeRuleConvention() {
            @Override
            public int getOrder() {
                return Ordered.LOWEST_PRECEDENCE;
            }

            @Override
            public List<AlternateTypeRule> rules() {
				List<AlternateTypeRule> rules = new ArrayList(1);
				rules.add(AlternateTypeRules.newRule(resolver.resolve(Pageable.class), resolver.resolve(Page.class)));
                return rules;
            }
        };
    }

    @Schema
    @Data
    static class Page {
        @Schema(description="第page页,从1开始计数")
        private Integer page;

        @Schema(description="每页数据数量")
        private Integer size;

        @Schema(description="按属性排序,格式:属性(,asc|desc)")
        private List<String> sort;
    }
}
